<?php

class People_PersonController extends Base_Controller_Action
{

    public function queryAction()
    {
        $form = new Form_Person_Query();
        $this->view->form = $form;
        
        if (!$this->_request->isPost()
            || !$form->isValid($this->_request->getPost())) {
            return;
        }
        
        //
        $dbTable = new Table_Person();
        $dbSelect = $dbTable->select();
        
        foreach ($form->person as $element) {
            if ($element->getValue()) {
                $dbSelect->where('`'.$element->getName().'`= ?', $this->view->translate($element->getValue()));
            }
        }

        //fetch data
        Zend_Paginator::setCache(Zend_Registry::get('Zend_Cache'));
        $this->view->paginator = Zend_Paginator::factory($dbSelect);

        //check page
        if (is_numeric($this->_getParam('page'))) {
            $this->view->paginator->setCurrentPageNumber($this->_getParam('page'));
        }
        
        $this->view->request = $this->getRequest();
    }
    
    public function createAction()
    {
        $family = new Model_Family();
        try {
            $family->read($this->_getParam('fid'));
        }
        catch (Base_Exception $e) {
            return $this->_helper->ErrorPage($e->getMessage());
        }
        
        //create form
        $this->form = new Form_Person_Create();
        
        try {
            $this->create('person');
        }
        catch (Base_Exception $e) {
            return $this->_helper->ErrorPage($e->getMessage());
        }
        
        //check oid
        $this->form->fid->setValue($family->fid);
        $this->view->family = $family;
    }
    
    public function modifyAction()
    {
        //create form
        $this->form = new Form_Person_Modify();
        
        try {
            $this->modify('person');
        }
        catch (Base_Exception $e) {
            return $this->_helper->ErrorPage($e->getMessage());
        }
    }
    
    public function deleteAction()
    {
        $this->form = new Form_Person_Delete();
        
        //drop post
        try {
            $this->delete('person');
        }
        catch (Base_Exception $e) {
            return $this->_helper->ErrorPage($e->getMessage());
        }
    }
}